from manim import *

class scene1(Scene):
    def construct(self):
        #down there is the set up area
        r = Rectangle()
        r.set_fill(BLUE, opacity=0.5)
        c = Circle()
        c.set_fill(PINK, opacity=0.4)
        sq = Square()
        sq.set_fill(YELLOW, opacity=0.5)
        tr = Triangle()
        tr.set_fill(GREEN, opacity=0.4)

        #Down there is the play area
        self.add(r)
        self.wait(2)
        self.play(ReplacementTransform(r, c))
        self.wait(2)
        self.play(ReplacementTransform(c, sq))
        self.wait(2)
        self.play(ReplacementTransform(sq, tr))
        self.play(tr.animate.shift(UP))
        self.play(tr.animate.shift(DOWN))
        self.play(tr.animate.shift(LEFT))
        self.play(tr.animate.shift(RIGHT))
        self.play(Rotate(tr, PI/3))
        self.remove(tr)

class scene2(Scene):
    def construct(self):
        T1 = MathTex("1")
        T2 = MathTex("1^2 = 1")
        T3 = MathTex("5x+3=8")
        T4 = MathTex("5x+3=y")

        self.play(Create(T1))
        self.wait(2)
        self.remove(T1)
        self.add(T2)
        self.wait(2)
        self.remove(T2)
        self.add(T3)
        self.wait(1)
        self.remove(T3)
        self.add(T4)
        self.play(FadeOut(T4))

class scene3(MovingCameraScene):
    def construct(self):
        ax = Axes(x_length=30, y_length=20, x_range=[-15, 15], y_range=[-10, 10], axis_config={"include_numbers": True})
        ax_labels = ax.get_axis_labels(x_label="x", y_label="y")
        y_5x_3 = ax.plot(lambda x:5*x+3)
        y_5x_3Tex = MathTex(r"y=5x+3")
        y_x2 = ax.plot(lambda x:x**2)
        y_x2Tex = MathTex(r"y=x^2")
        yis0point5x2 = ax.plot(lambda x:0.5*(x**2)) 
        yis0point5x2Tex = MathTex(r"y=\frac{1}{2}x^2")
        yis0poitnt25x2 = ax.plot(lambda x:0.25*(x**2))
        yis0poitnt25x2Tex = MathTex(r"y=\frac{1}{4}x^2")
        negativeyis0point25x2 = ax.plot(lambda x:-0.25*(x**2))
        yisx2plus5 = ax.plot(lambda x:x**2+5)
        x_tracker = ValueTracker(0) # 添加坐标tracker
        y_tracker = ValueTracker(5)
        top_dot_NY25 = always_redraw(lambda: Dot(ax.c2p(x_tracker.get_value(), y_tracker.get_value())))# 这样出来的点就可以更改位置了
        def get_coordinate_label():
            x_val = int(x_tracker.get_value())  # 转换为整数
            y_val = int(y_tracker.get_value())  # 转换为整数
            return MathTex(f"({x_val}, {y_val})")# 返回一个MathTex
        
        # 创建总是更新的坐标标签
        coordinate_label = always_redraw(lambda: get_coordinate_label().next_to(# 调用上面的函数，获得坐标文本
            ax.c2p(x_tracker.get_value(), y_tracker.get_value()), 
            DOWN + RIGHT # 把文本放在指定位置的指定方向
        ))
        Bpoint = Dot(ax.c2p(10, -4))
        Tex_A = Tex("A")
        Tex_B = Tex("B")
        LineAB = Line(ax.c2p(-7, -4), Bpoint.get_center())
        ABrec = Rectangle(width=2, 
                          height=4, 
                          color=BLUE, 
                          fill_color=BLUE, 
                          fill_opacity=0.5)
        square3 = Square(side_length=2, 
                         color=YELLOW, 
                         fill_color=YELLOW, 
                         fill_opacity=0.5)
        square3.move_to(ax.c2p(13, 0))
        triangle3 = Triangle(fill_color = YELLOW, 
                             fill_opacity = 0.5)
        Alinetohit = Line(ax.c2p(14, 9), ax.c2p(7, 9))
        Finalsquare = Square(side_length=3, 
                             fill_color=GREEN, 
                             fill_opacity=0.6)
        Finalsquare.move_to(ax.c2p(-14, -8))

        self.play(Create(ax), Write(ax_labels))
        self.play(self.camera.auto_zoom(ax))
        self.play(Create(y_5x_3))
        self.play(Write(y_5x_3Tex.move_to(ax.c2p(2, 3))))
        self.play(ReplacementTransform(y_5x_3, y_x2), 
                  ReplacementTransform(y_5x_3Tex, y_x2Tex.shift(DOWN)))
        self.play(ReplacementTransform(y_x2, yis0point5x2), 
                  ReplacementTransform(y_x2Tex, yis0point5x2Tex.shift(DOWN)))
        self.play(ReplacementTransform(yis0point5x2, yis0poitnt25x2), 
                  ReplacementTransform(yis0point5x2Tex, yis0poitnt25x2Tex.shift(DOWN)))
        self.play(yis0poitnt25x2.animate.shift(5*DOWN), 
                  yis0poitnt25x2Tex.animate.shift(15*UP))
        self.remove(yis0poitnt25x2Tex)
        self.play(ReplacementTransform(yis0poitnt25x2,negativeyis0point25x2))
        self.play(negativeyis0point25x2.animate.shift(5*UP))
        self.play(ReplacementTransform(negativeyis0point25x2, yisx2plus5))
        self.play(Create(top_dot_NY25), 
                  Write(coordinate_label)) # 添加这个超难的玩意，下面的代码是为了移动它
        self.play(x_tracker.animate.set_value(-5), 
                  y_tracker.animate.set_value(0), 
                  FadeOut(yisx2plus5), 
                  run_time=0.5)# ~~为什么只有坐标在移动，我点呢~~现在没事了
        self.wait(0.5)
        self.play(x_tracker.animate.set_value(-1), 
                  y_tracker.animate.set_value(-5), 
                  run_time=0.5)
        self.play(x_tracker.animate.set_value(14), 
                  y_tracker.animate.set_value(-7), 
                  run_time=1)
        self.wait(0.5)
        self.play(x_tracker.animate.set_value(-7), 
                  y_tracker.animate.set_value(-4), 
                  run_time=1)
        self.play(FadeOut(coordinate_label), 
                  Write(Tex_A.next_to(top_dot_NY25, DOWN+LEFT)), 
                  Create(Bpoint), 
                  Write(Tex_B.next_to(Bpoint, DOWN+RIGHT)))#  超难的部分结束了，现在请欣赏下一段超难的部分
        self.play(Create(LineAB))# 点A，点B交朋友，让我来帮你连个线
        self.play(FadeOut(top_dot_NY25),
                  FadeOut(Tex_A),
                  FadeOut(Bpoint),
                  FadeOut(Tex_B),
                  run_time = 0.5)
        self.play(Rotate(LineAB, angle=270*DEGREES))
        self.play(LineAB.animate.shift(7*UP+10*LEFT))
        self.play(Rotate(LineAB, 45*DEGREES))
        self.play(LineAB.animate.shift(7*DOWN+10*RIGHT), run_time=0.5)
        self.play(LineAB.animate.shift(7*UP+10*LEFT), run_time=0.5)
        self.play(ReplacementTransform(LineAB, ABrec))
        self.play(ABrec.animate.move_to(ax.c2p(10, 0)), 
                  FadeIn(square3))
        self.play(square3.animate.shift(8*DOWN), 
                  run_time=0.5)
        self.play(ABrec.animate.shift(8*DOWN), 
                  run_time=0.5) 
        self.play(ABrec.animate.shift(8*UP), 
                  run_time=0.5)
        self.play(ReplacementTransform(square3, triangle3.move_to(ax.c2p(13, -8))))
        self.play(triangle3.animate.shift(16*UP))
        self.play(Create(Alinetohit))
        self.play(FadeOut(Alinetohit))
        self.play(ABrec.animate.move_to(ax.c2p(6, 0)), 
                  run_time=0.5)
        self.play(ABrec.animate.shift(8*DOWN), 
                  run_time=0.5)
        self.play(ABrec.animate.shift(8*UP), 
                  run_time=0.5)
        self.play(FadeOut(triangle3))
        self.play(Create(Finalsquare))
        self.play(Finalsquare.animate.move_to(ax.c2p(2, -8)), 
                  run_time=0.5)

        # test where to write the Tex
        # self.play(self.camera.auto_zoom(y_5x_3Tex))
        self.wait(2)